<?php
/**
 * @author: Đặng Văn Trường
 * @since: 22/07/2014
 * @todo : Model Van ban den
 */
class Module_Models_DocReceive extends Zend_Db_Table_Abstract {	
	
	protected $_name = "docreceive";
    protected $_primary_name;
    protected $_primary = "ReceiveID";
    protected $_detail = 'receivedetail';
    protected $_department = "receivehandledepartment";
    protected $_staff = "receivehandlestaff";
    public function __construct()
    {
        parent::__construct();
        $this->_primary_name = $this->_primary;
    }
	public function getAll($params) {

        $pagenum = $params['pagenum'];
        $pagesize = $params['pagesize'];
        $start = $pagenum * $pagesize;
        $status = $params['Status'];
        $where ='1=1';
        if($status){
            $where .=" And Status='$status'";
        }
        // Filter
        if (isset($params['filterscount']))
        {
            $filterscount = $params['filterscount'];
            
            if ($filterscount > 0)
            {
                $where .= " And (";
                $tmpdatafield = "";
                $tmpfilteroperator = "";
                for ($i=0; $i < $filterscount; $i++)
                {
                    // get the filter's value.
                    $filtervalue = $params["filtervalue" . $i];
                    // get the filter's condition.
                    $filtercondition = $params["filtercondition" . $i];
                    // get the filter's column.
                    $filterdatafield = $params["filterdatafield" . $i];
                    // get the filter's operator.
                    $filteroperator = $params["filteroperator" . $i];
                    
                    if ($tmpdatafield == "")
                    {
                        $tmpdatafield = $filterdatafield;           
                    }
                    else if ($tmpdatafield <> $filterdatafield)
                    {
                        $where .= ")AND(";
                    }
                    else if ($tmpdatafield == $filterdatafield)
                    {
                        if ($tmpfilteroperator == 0)
                        {
                            $where .= " AND ";
                        }
                        else $where .= " OR ";  
                    }
                    
                    // build the "WHERE" clause depending on the filter's condition, value and datafield.
                    $where .= Clound_Public::getInstance()->getClauseByFilter($filtercondition, $filterdatafield, $filtervalue);
                                    
                    if ($i == $filterscount - 1)
                    {
                        $where .= ")";
                    }
                    
                    $tmpfilteroperator = $filteroperator;
                    $tmpdatafield = $filterdatafield;      
                }
            }
        }
        // Sort
        $orders =' Number DESC';
        if (isset($params['sortdatafield'])) {
            $sortfield = $params['sortdatafield'];
            $sortorder = $params['sortorder'];
            if ($sortorder != '') {
                if ($sortorder == "desc") {
                    $orders = " " . $sortfield . " DESC";
                } else if ($sortorder == "asc") {
                    $orders = " " . $sortfield . " ASC";
                }
            }
        }
        try {
            // Lay tong
            $getTotal = $this->select()->from($this, array())->columns(array('total' => new Zend_Db_Expr('COUNT(*)')))->where($where);
            $rows = $this->getAdapter()->fetchRow($getTotal);
            $total_rows = $rows['total'];
            // Lay du lieu
            $query = $this->select()->from($this)->where($where)->order($orders)->limit($pagesize, $start);
            $rows = $this->getAdapter()->fetchAll($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        $data[] = array(
            'TotalRows' => $total_rows,
            'Rows' => $rows
        );

        return $data;
    }
	
    public function selectRow($id){
        if($id){
            $query = $this->select()->from($this)->where($this->_primary_name.' = ?', $id);
            $rows = $this->getAdapter()->fetchRow($query);
            $rows['ReceivedDate'] = date('d/m/Y',strtotime($rows['ReceivedDate']));
            $rows['SendDate'] = date('d/m/Y',strtotime($rows['SendDate']));
            //Thong tin file dinh kem
            $rows['RecordID'] = $id.".$this->_name.RECEIVE";
        }else{
            $rows = array($this->_primary_name =>''
                            ,'ReceivedDate'=> date('d/m/Y')
                            ,'Year' => date('Y')
                        );
        }
        return $rows;
    }

    public function deleteRows($listId){
        $aListID = explode(',', $listId);
        // Xoa file dinh kem
        // $modeFile = new System_Models_File();
        try {
            $where = $this->getAdapter()->quoteInto("$this->_primary_name IN (?)", $aListID);
            $result = $this->delete($where);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $result;
        // echo $where; die;
    }

    /**
     * Tranfer staff assign
     *
     * @param  array $options
     * @throws Zend_Application_Exception When no bootstrap path is provided
     * @throws Zend_Application_Exception When invalid bootstrap information are provided
     * @return Zend_Application
     */
    public function assign(){
        // Xac dinh can bo duoc phan cong
        // Cap nhat bang phong ban
        // Cap nhat can bo
    }

    public function getassign($params) {

        $pagenum = $params['pagenum'];
        $pagesize = $params['pagesize'];
        $start = $pagenum * $pagesize;
        $status = $params['Status'];
        $where ='1=1';
        if($status){
            $where .=" And B.Status='$status'";
        }
        $StaffID = Zend_Auth::getInstance()->getIdentity()->StaffID;
        $where .=" And B.StaffID = '$StaffID'";
        // Filter
        if (isset($params['filterscount']))
        {
            $filterscount = $params['filterscount'];
            
            if ($filterscount > 0)
            {
                $where .= " And (";
                $tmpdatafield = "";
                $tmpfilteroperator = "";
                for ($i=0; $i < $filterscount; $i++)
                {
                    // get the filter's value.
                    $filtervalue = $params["filtervalue" . $i];
                    // get the filter's condition.
                    $filtercondition = $params["filtercondition" . $i];
                    // get the filter's column.
                    $filterdatafield = $params["filterdatafield" . $i];
                    // get the filter's operator.
                    $filteroperator = $params["filteroperator" . $i];
                    
                    if ($tmpdatafield == "")
                    {
                        $tmpdatafield = $filterdatafield;
                    }
                    else if ($tmpdatafield <> $filterdatafield)
                    {
                        $where .= ")AND(";
                    }
                    else if ($tmpdatafield == $filterdatafield)
                    {
                        if ($tmpfilteroperator == 0)
                        {
                            $where .= " AND ";
                        }
                        else $where .= " OR ";  
                    }
                    $filterdatafield = 'A.'.$filterdatafield;
                    // build the "WHERE" clause depending on the filter's condition, value and datafield.
                    $where .= Clound_Public::getInstance()->getClauseByFilter($filtercondition, $filterdatafield, $filtervalue);
                                    
                    if ($i == $filterscount - 1)
                    {
                        $where .= ")";
                    }
                    
                    $tmpfilteroperator = $filteroperator;
                    $tmpdatafield = $filterdatafield;           
                }
            }
        }
        // Sort
        $orders = ' A.Number DESC';
        if (isset($params['sortdatafield'])) {
            $sortfield = $params['sortdatafield'];
            $sortorder = $params['sortorder'];
            if ($sortorder != '') {
                if ($sortorder == "desc") {
                    $orders = " A." . $sortfield . " DESC";
                } else if ($sortorder == "asc") {
                    $orders = " A." . $sortfield . " ASC";
                }
            }
        }
        // Lay tong
        $getTotal = $this->getAdapter()->select()
                        ->from(array('A' => $this->_name),array('Number','Symbol'))
                        ->join(array('B' => 'receivehandlestaff'),'A.ReceiveID = B.DocID',array())
                        ->columns(array('total' => new Zend_Db_Expr('COUNT(*)')))
                        ->where($where);
        try {
            $rows = $this->getAdapter()->fetchRow($getTotal);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        $total_rows = $rows['total'];
        
        // Lay du lieu
        $query = $this->getAdapter()->select()
                        ->from(array('A' => $this->_name))
                        ->join(array('B' => 'receivehandlestaff'),'A.ReceiveID = B.DocID',array())
                        ->where($where)
                        ->order($orders)
                        ->limit($pagesize, $start);
        try {
            $rows = $this->getAdapter()->fetchAll($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        $data[] = array(
            'TotalRows' => $total_rows,
            'Rows' => $rows
        );

        return $data;
    }
}
?>
